package com.cn.kehong.service;


import com.cn.kehong.api.IDataSubmissionCcService;
import com.cn.kehong.dao.DataSubmissionCcMapper;
import com.cn.kehong.domain.DataSubmissionCc;
import com.cn.kehong.domain.DataSubmissionCcExample;
import com.cn.kehong.domain.DataSubmissionVo;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataSubmissionCcServiceImpl implements IDataSubmissionCcService {

    @Autowired
    private DataSubmissionCcMapper dataSubmissionCcMapper;

    @Override
    public List<DataSubmissionVo> getDataSubmissionVoList(QueryForm queryForm) throws Exception {
        int count = dataSubmissionCcMapper.selectDataSubmissionCcListCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<DataSubmissionVo> dataSubmissions = dataSubmissionCcMapper.selectDataSubmissionCcList(queryForm);
        dataSubmissions.get(0).setCount(count);
        return dataSubmissions;
    }

    @Override
    public List<DataSubmissionCc> getDataSubmissionCcListById(String submissionId) throws Exception {
        if(StringUtils.isEmpty(submissionId)){
            return Lists.newArrayList();
        }
        DataSubmissionCcExample example = new DataSubmissionCcExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andSubmissionIdEqualTo(submissionId);
        example.setOrderByClause("sort_index asc");
        List<DataSubmissionCc> list = dataSubmissionCcMapper.selectByExample(example);
        return list;
    }

    @Override
    public String addDataSubmissionCc(List<DataSubmissionCc> dataSubmissionCcs) throws Exception {
        if(CollectionUtils.isEmpty(dataSubmissionCcs)){
            return "数组为空！";
        }
        int count = dataSubmissionCcMapper.batchInsertDataSubmissionCc(dataSubmissionCcs);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateDataSubmissionCc(List<DataSubmissionCc> dataSubmissionCcs) throws Exception {
        if(CollectionUtils.isEmpty(dataSubmissionCcs)){
            return "数组为空！";
        }
        String submissionId = dataSubmissionCcs.get(0).getSubmissionId();
        if(StringUtils.isEmpty(submissionId)){
            return "id 为空！";
        }
        DataSubmissionCcExample example = new DataSubmissionCcExample();
        example.createCriteria().andSubmissionIdEqualTo(submissionId);
        int count = dataSubmissionCcMapper.deleteByExample(example);
        if(count < 0){
            return "清理数据异常！";
        }
        count = dataSubmissionCcMapper.batchInsertDataSubmissionCc(dataSubmissionCcs);
        if(count < 1){
            throw new Exception("插入数据异常！");
        }
        return "";
    }

}
